package com.ys.report.controller;

import com.ys.InvoiceExporter;
import com.ys.TemplateFileInfo;
import com.ys.enums.ExportEnum;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class ReportDemoController {

	@GetMapping("/export")
	public void export(HttpServletRequest request, HttpServletResponse response) throws Exception{
		// 模拟结果集
		Map<String, Object> data = getData();
		InvoiceExporter invoiceExporter = new InvoiceExporter();
		Map<String,Object> headMap = new HashMap<>();
		headMap.put("invoiceDate","2024-12-19");
		headMap.put("recipient","王涛");
		headMap.put("quantity",111);
		headMap.put("unitPrice",222);
		headMap.put("orderNo","YSNBSQ-1");
		TemplateFileInfo templateFileInfo = new TemplateFileInfo();
		headMap.put("templateFileInfo",templateFileInfo);
		invoiceExporter.export(headMap, data,10, ExportEnum.EXCEL);

//		Workbook workbook = ExcelExportUtil.exportExcel(params, data);
//		File savefile = new File("D:\\JAVA\\poi");
//		if (!savefile.exists()) {
//			savefile.mkdirs();
//		}
//		FileOutputStream fos = new FileOutputStream("D:\\JAVA\\poi\\测试模板导出.xlsx");
//		workbook.write(fos);
//		fos.close();
		download("D:\\JAVA\\poi\\测试模板导出.xlsx", request, response);
	}

	/**
	 * 创建数据集
	 *
	 * @return
	 */
	private Map<String, Object> getData() {
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("company", "硕哥帅气牛逼但责任有限公司");
		dataMap.put("address", "山西省山东市");
		dataMap.put("businessNumber", "132432412343SF");
		// 本业金额
		dataMap.put("nowTotalUp", 1314.00);
		// 本页小计
		dataMap.put("nowTotalLow", 1314.00);
		dataMap.put("allUp", 1314.00);
		dataMap.put("allLow", 1314.00);
		List<Map<String, Object>> listMap = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			Map<String, Object> tempMap = new HashMap<>();
			tempMap.put("id", i + 1);
			tempMap.put("name", "商品" + (i + 1));
			tempMap.put("guige", "YS_PC-" + (i + 1));
			tempMap.put("unit", "车");
			tempMap.put("num", 2);
			tempMap.put("price", 116);
			tempMap.put("totalCash", 232);
			tempMap.put("entrustCompany", "硕哥帅气牛逼但责任有限公司");
			tempMap.put("productCompany", "涛哥不帅气不牛逼但责任无限公司");
			tempMap.put("productLicence", "YSNBSQZRYXGS" + i);
			tempMap.put("serialNumber", "YSNBSQ-" + i);
//			Map<String,Object> map = new HashMap<>();
//			map.put("manufactureDate", "2024-12-19");
//			map.put("expirationDate", "2026-12-19");
			tempMap.put("consignee", "王涛18346789876");
			tempMap.put("conditions", "冷库");
//			tempMap.put("time",map);
			listMap.add(tempMap);
		}
		dataMap.put("datas", listMap);
		return dataMap;
	}


	/**
	 * @comment: 下载文件
	 * @param:
	 * @author: 傅里叶级数
	 * @date 2018年5月27日上午10:25:45
	 */
	public static void download(String path, HttpServletRequest request, HttpServletResponse response) {
		try {
			File file = new File(path);
			if (!file.exists()) {
				System.out.println("文件不存在");
				return;
			}
			String filename = file.getName();
			InputStream fis = new BufferedInputStream(new FileInputStream(file));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.reset();
			response.addHeader("Content-Disposition", "attachment;filename="+new String(filename.getBytes("utf-8"), "ISO-8859-1"));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
